package com.club.chelseaclub.tab;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.club.chelseaclub.R;
import com.club.chelseaclub.util.DownLoadImage;
import com.club.chelseaclub.util.SinaNewsRestClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class NewsDetailActivity extends Activity {
	
	private WebView webview; 
	private TextView news_detail_title;
	private ImageView news_detail_image;
	private TextView news_detail_content;
	private ProgressBar loading_progressBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		String newsUrl = intent.getStringExtra("url");
		//catchNewsDetailContentByUrl(newsUrl);
		setContentView(R.layout.news_detail);
		
		webview = (WebView) findViewById(R.id.webview); 
        //设置WebView属性，能够执行Javascript脚本 
        webview.getSettings().setJavaScriptEnabled(true); 
        //加载需要显示的网页 
        webview.loadUrl(newsUrl); 
        //设置Web视图 
        webview.setWebViewClient(new HelloWebViewClient ()); 
		
		
		/*
		news_detail_title= (TextView) findViewById(R.id.news_detail_title);
		news_detail_image= (ImageView) findViewById(R.id.news_detail_image);
		news_detail_content = (TextView) findViewById(R.id.news_detail_content);
		loading_progressBar = (ProgressBar) findViewById(R.id.loading_process_dialog_progressBar);
		*/
	}
	
	/*
	@Override
    //设置回退 
    //覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { 
            webview.goBack(); //goBack()表示返回WebView的上一页面 
            return true; 
        } 
        return false; 
    } 
    */
     
    //Web视图 
    private class HelloWebViewClient extends WebViewClient { 
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) { 
            view.loadUrl(url); 
            return true; 
        } 
    } 
    
	
	private void catchNewsDetailContentByUrl(String url) {
		
		SinaNewsRestClient.get(url, null, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String response) {
				Document doc = Jsoup.parse(response);
				Element newsTitle = doc.select("h2").first();
				Element newsContent = doc.select("div.articleContent").first();
				if(newsContent != null) {
					String imgUrl = null;
					Element imageEle = newsContent.getElementsByTag("img").first();
					if(imageEle != null) {
						imgUrl = imageEle.attr("src");
					}
					news_detail_title.setText(Html.fromHtml(newsTitle.toString()));
					newsContent.getElementsByTag("a").remove();
					news_detail_content.setText(Html.fromHtml(newsContent.toString()));
					loading_progressBar.setVisibility(View.GONE);
					//news_detail_image.setImageBitmap(returnBitMap(imgUrl));
					if(imgUrl != null && !"".equals(imgUrl)) {
						new DownLoadImage(news_detail_image).execute(imgUrl);
					}
				}
			}
			
			@Override
			public void onFailure(Throwable e, String response) {
				e.printStackTrace();
				//stopProgress(l, v, position);
				Log.d("===通信错误===", "通信错误response: " + response);
				Toast.makeText(getApplication(), "通信错误", Toast.LENGTH_SHORT).show();
			}
		});
		
	}
	
}
